//
//  Menu2ViewController.swift
//  iModern
//
//  Created by Ralf on 8/17/15.
//  Copyright (c) 2015 com.starlooking. All rights reserved.
//

import UIKit

class Menu2ViewController: ViewController
{
    var appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

    let contactManage = ContactManager()
    
    var contacts:  [String:AnyObject]!
    
    @IBOutlet weak var cellPhoneLabel:  UILabel!
    @IBOutlet weak var modifyCellPhone: UIButton!
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?)
    {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }

    convenience init()
    {
        let nibNameOrNil = String?("Menu2ViewController")

        self.init(nibName: nibNameOrNil, bundle: nil)
    }
    required init(coder aDecoder: NSCoder)
    {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        self.loadData()
        
        initBackButton()
        self.title = "设置"
        self.setdata()
        
        // Do any additional setup after loading the view.
    }
    func setdata()
    {
//        let contact = appDelegate.contactManager.contact
//        var cellphone = contact["cellphone"] as? String
//        
//        if cellphone == nil || cellphone!.isEmpty
//        {
//            
//        }
//        else
//        {
//            modifyCellPhone.enabled = false
//            modifyCellPhone.setTitle("已绑定手机号", forState: UIControlState.Normal)
//            cellPhoneLabel.text = cellphone
//        }
        
//        var rs: ResultSet?
//        let userName = appDelegate.contactManager.contact["userName"] as! String
//        rs = contactManage.loadContact()
//        
//        if  rs!.statusCode == 200
//        {
////            self.contact = rs.toDictionary
//            self.contact = rs.toDictionary() as! [String:AnyObject]
//        }
        
        
        
        let cellphone = contacts["cellphone"] as? String
        
        if cellphone == nil || cellphone!.isEmpty
        {
            
        }
        else
        {
            modifyCellPhone.enabled = false
            modifyCellPhone.setTitle("已绑定手机号", forState: UIControlState.Normal)
            cellPhoneLabel.text = cellphone
        }
        
    }

    func loadData()
    {
        let rs = appDelegate.contactManager.loadContact()
        
        if self.verifyResutSet(rs)
        {
            if (!rs.isEmpty())
            {
                self.contacts = rs.toDictionary() as! [String:AnyObject]
                print(contacts["cellphone"] as? String)
            }
        }
    }
    
    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func modifyCellPhone(sender: UIButton)
    {
        let controller = ModifyCellphoneViewController()
        self.pushViewController(controller)
    }

    @IBAction func modifyPassword(sender: UIButton)
    {
        let controller = ModifyPasswordViewController()
        self.pushViewController(controller)
    }

    @IBAction func logout(sender: UIButton)
    {
        appDelegate.contactManager.logout()
        NSNotificationCenter.defaultCenter().postNotificationName("Logout", object: nil)
       

        self.navigationController?.popToRootViewControllerAnimated(true)
        NSNotificationCenter.defaultCenter().postNotificationName("Refresh Shop2", object: nil)
    }

    @IBAction func call(sender: AnyObject)
    {
        UIApplication.sharedApplication().openURL(NSURL(string: "tel://4000060155")!)
    }
}
